//
//  CommentTableViewCell.m
//  EveryDay
//
//  Created by tidoo on 16/4/20.
//  Copyright © 2016年 张广龙. All rights reserved.
//

#import "SayCommentTableViewCell.h"
#import "ImageAndTitleButton.h"

@interface SayCommentTableViewCell ()

@property(nonatomic,weak)UIImageView *titleicon;

@property(nonatomic,weak)UILabel *nameLabel;

@property(nonatomic,weak)UILabel *topLabel;

@property(nonatomic,weak)UILabel *dateLabel;

@property(nonatomic,weak)ImageAndTitleButton *zanButton;

@property(nonatomic,weak)ImageAndTitleButton *replayButton;

@property(nonatomic,weak)UIImageView *zanDetailView;

@property(nonatomic,weak)UILabel *zanPeopleLabel;

@property(nonatomic,weak)UILabel *commentLabel;

@property(nonatomic,weak)UIView *commentImageView;

@property (nonatomic,weak) UIButton *deleteButton;

@property (nonatomic,weak) UIButton *scanMoreButton;

@property (nonatomic,weak) UIImageView *triangleimage;

@property(nonatomic,weak)UIView *midView;

@property (nonatomic,weak) UIView *zanView;

@property(nonatomic,weak)UIView *replayView;

@property(nonatomic,weak)UIView *lineView;

@end

@implementation SayCommentTableViewCell
- (void)setFrame:(CGRect)frame {

    if (frame.origin.x==0) {
        frame.origin.x += 10;
    }
    if (frame.size.width == kScreenW) {
        
        frame.size.width -= 2 * 10;
    }

    [super setFrame:frame];

}
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"comment";
    SayCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        
        cell = [[SayCommentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
//        cell.backgroundColor = KBACKGROUNDCOLOR;
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle=UITableViewCellSelectionStyleNone;
        self.clipsToBounds = YES;
        self.layer.cornerRadius = 10;
        
        UIImageView *iconimage=[[UIImageView alloc]init];
        iconimage.layer.masksToBounds=YES;
        [self.contentView addSubview:iconimage];
        self.titleicon=iconimage;
        
        UILabel *namelable=[[UILabel alloc]init];
        namelable.font=[UIFont systemFontOfSize:16];
        namelable.textColor=RGBColor(32, 112, 221);
        [self.contentView addSubview:namelable];
        self.nameLabel=namelable;
        
        UILabel *toplabel=[[UILabel alloc]init];
        toplabel.textAlignment=NSTextAlignmentCenter;
        toplabel.textColor=KTEXTBLACKCOLOR;
        toplabel.text=@"置顶";
        [self.contentView addSubview:toplabel];
        self.topLabel=toplabel;
        
        UILabel *datelabel=[[UILabel alloc]init];
        datelabel.font=[UIFont systemFontOfSize:14];
        datelabel.numberOfLines=0;
        [self.contentView addSubview:datelabel];
        self.dateLabel=datelabel;
        
        UIButton *deleteButton = [[UIButton alloc]init];
        deleteButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [deleteButton setTitle:@"删除" forState: UIControlStateNormal];
        [deleteButton setTitleColor:RGBColor(32, 112, 221) forState:UIControlStateNormal];
        [self.contentView addSubview:deleteButton];
        self.deleteButton = deleteButton;
        
        UIButton *scanMoreButton = [[UIButton alloc]init];
        scanMoreButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [scanMoreButton setTitle:@"查看全文" forState: UIControlStateNormal];
        [scanMoreButton setTitleColor:RGBColor(32, 112, 221) forState:UIControlStateNormal];
        [self.contentView addSubview:scanMoreButton];
        self.scanMoreButton = scanMoreButton;
        
        ImageAndTitleButton *zanbutton=[[ImageAndTitleButton alloc]init];
        [zanbutton setImage:[UIImage imageNamed:@"listZan.png"] forState:UIControlStateNormal];
        [zanbutton setImage:[UIImage imageNamed:@"clubsgreenZan"] forState:UIControlStateSelected];
        zanbutton.tag=ClickZan;
        [zanbutton setTitleColor:KTEXTBLACKCOLOR forState:UIControlStateNormal];
        zanbutton.titleLabel.font = [UIFont systemFontOfSize:12];
        [zanbutton addTarget:self action:@selector(clickZanOrReplay:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:zanbutton];
        self.zanButton=zanbutton;
        
        ImageAndTitleButton *replaybutton=[[ImageAndTitleButton alloc]init];
        [replaybutton setImage:[UIImage imageNamed:@"listReply.png"] forState:UIControlStateNormal];
        [replaybutton setTitleColor:KTEXTBLACKCOLOR forState:UIControlStateNormal];
        replaybutton.tag=ClickReplay;
        replaybutton.titleLabel.font = [UIFont systemFontOfSize:12];
        [replaybutton addTarget:self action:@selector(clickZanOrReplay:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:replaybutton];
        self.replayButton=replaybutton;
        
        UILabel *commentlable=[[UILabel alloc]init];
        commentlable.font=[UIFont systemFontOfSize:14];
        commentlable.numberOfLines=0;
        [self.contentView addSubview:commentlable];
        self.commentLabel=commentlable;
        
        UIView *commentimage=[[UIView alloc]init];
        [self.contentView addSubview:commentimage];
        self.commentImageView=commentimage;
        
        UIImageView *zanDetailView=[[UIImageView alloc]init];
        [self.contentView addSubview:zanDetailView];
        self.zanDetailView=zanDetailView;
        
        UIImageView *triangleimage=[[UIImageView alloc]init];
        triangleimage.image = [UIImage imageNamed:@"img_triangle"];
        [self.contentView addSubview:triangleimage];
        self.triangleimage=triangleimage;
        
        UIView *replayView=[[UIView alloc]init];
        replayView.backgroundColor = KTEXTGRAYLIGHTCOLOR;
        [self.contentView addSubview:replayView];
        self.replayView=replayView;
        
        UIView *zanView=[[UIView alloc]init];
        zanView.backgroundColor = KTEXTGRAYLIGHTCOLOR;
        [self.contentView addSubview:zanView];
        self.zanView=zanView;
        
        UILabel *zanPeopleLabel=[[UILabel alloc]init];
        zanPeopleLabel.font=[UIFont systemFontOfSize:14];
        zanPeopleLabel.numberOfLines = 0;
        zanPeopleLabel.textColor=RGBColor(32, 112, 221);
        zanPeopleLabel.backgroundColor = KTEXTGRAYLIGHTCOLOR;
        [self.contentView addSubview:zanPeopleLabel];
        self.zanPeopleLabel=zanPeopleLabel;
        
        UIView *midline=[[UIView alloc]init];
        midline.backgroundColor=[UIColor lightGrayColor];
        [self.contentView addSubview:midline];
        self.midView=midline;
        
//        UIView *lineView=[[UIView alloc]init];
//        lineView.backgroundColor=[UIColor lightGrayColor];
//        [self.contentView addSubview:lineView];
//        self.lineView=lineView;
        
    }
    return self;
}

- (void)setCommentframe:(CommentFrame *)commentframe
{
    _commentframe=commentframe;
    CommentInfo *commentinfo=commentframe.commentinfo;
    //    self.topLabel.hidden=![commentinfo.istop boolValue];
    self.titleicon.frame=commentframe.titleiconF;
    self.titleicon.layer.cornerRadius=CGRectGetWidth(self.titleicon.bounds)/2,0;
    [self.titleicon sd_setImageWithURL:[NSURL URLWithString:commentinfo.icon] placeholderImage:KDEFAULTHEAD];
    
    self.nameLabel.frame=commentframe.nameF;
    self.nameLabel.text=strIsEmpty(commentinfo.nick_name)?commentinfo.name:commentinfo.nick_name;
    
    if ([commentinfo.is_ad isEqualToString:@"0"]) {
        self.dateLabel.frame= commentframe.dateF;
        self.dateLabel.text= [Global callBackDateTime:commentinfo.add_time];
        
        self.zanButton.frame=commentframe.zanF;
        [self.zanButton setTitle:@"赞" forState:UIControlStateNormal];
        [self.zanButton setTitle:@"赞" forState:UIControlStateSelected];
        self.zanButton.selected=[commentinfo.praise_history boolValue];
        
        self.replayButton.frame=commentframe.replayF;
        [self.replayButton setTitle:@"评论" forState:UIControlStateNormal];
        
        self.commentLabel.frame=commentframe.commentF;
        self.commentLabel.text=commentinfo.topic_context;
        
        self.zanDetailView.frame = commentframe.zanDetailF;
        self.scanMoreButton.frame = CGRectZero;
    }else{
        self.dateLabel.frame= CGRectZero;
        self.dateLabel.text=@"";
        
        self.zanButton.frame=CGRectZero;
        self.replayButton.frame=CGRectZero;
        self.commentLabel.frame=CGRectZero;
        self.zanDetailView.frame = CGRectZero;
        self.scanMoreButton.frame = commentframe.scanMoreF;
        [self.scanMoreButton addTarget:self action:@selector(scanMore:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
    if ([commentinfo.user_id isEqualToString:[JHUserInfo getUserInfo].userId]) {
        self.deleteButton.frame = commentframe.deleteF;
    }else{
        self.deleteButton.frame = CGRectZero;
    }
    [self.deleteButton addTarget:self action:@selector(deleteCommentReplay) forControlEvents:UIControlEventTouchUpInside];
    
    
   
    self.triangleimage.frame = commentframe.triangleimageF;
    
    self.zanPeopleLabel.frame = commentframe.zanPeoPleF;
    
    
    NSMutableArray *pariseArray = [NSMutableArray array];
    [commentinfo.pariseList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [pariseArray addObject:obj[@"nick_name"]];
    }];
    self.zanPeopleLabel.font = [UIFont fontWithName:@"IconFont" size: 12.0];
    self.zanPeopleLabel.text = [NSString stringWithFormat:@"\U0000e607 %@", [pariseArray componentsJoinedByString:@","]];
    if (strIsEmpty(self.zanPeopleLabel.text)) {
        self.zanView.frame = CGRectZero;
    }else{
        self.zanView.frame = commentframe.zanViewF;
    }
    if (!commentinfo.image_list.count || strIsEmpty(commentinfo.image_list[0])) {
        self.commentImageView.frame=CGRectZero;
        [self removeallSubViews:self.commentImageView];
    }
    else
    {
        [self removeallSubViews:self.commentImageView];
        self.commentImageView.frame=commentframe.commentImageF;
        for (int i=0; i < commentinfo.image_list.count; i++) {
            double allWidth = CGRectGetWidth(commentframe.commentImageF);
            double oneWidth = (allWidth - 10)/3;
            UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(( oneWidth + 5) * (i%3), (oneWidth + 5) * (i / 3), oneWidth, oneWidth)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:commentinfo.image_list[i]] placeholderImage:KRECOMMENDLISTDEFAULT];
            imageView.tag=100+i;
            imageView.backgroundColor= [UIColor clearColor];
            imageView.contentMode=UIViewContentModeScaleToFill;
            [self.commentImageView addSubview:imageView];
            
            imageView.userInteractionEnabled=YES;
            UITapGestureRecognizer *imagetap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickImage:)];
            [imageView addGestureRecognizer:imagetap];
        }
    }
    if (!commentinfo.comment_list.count) {
        self.midView.frame=CGRectZero;
        [self removeallSubViews:self.replayView];
        self.replayView.frame= CGRectZero;
    }
    else{
        self.midView.frame=commentframe.midlineF;
        self.replayView.frame=commentframe.replayViewF;
        [self removeallSubViews:self.replayView];
        CGFloat replaylabelY=5;
        NSInteger count=commentinfo.comment_list.count ;
        for (int i=0; i<count; i++) {//回复评论高度
            ReplayInfo *replayinfo=commentinfo.comment_list[i];
            NSString *name=replayinfo.user_name;
            NSString *repName ;
            NSString *content=replayinfo.content;
            NSString *showStr = [NSString stringWithFormat:@"%@ :  %@",name,content];
            if (!strIsEmpty(replayinfo.to_user_name)) {
                repName = replayinfo.to_user_name;
                showStr =  [NSString stringWithFormat:@"%@回复%@:  %@",name,repName,content];
            }
            CGFloat replayH=[Global getSizeOfString:showStr maxWidth:CGRectGetWidth(self.replayView.frame)-5 maxHeight:CGFLOAT_MAX withFontSize:14].height+5;
            
            UIButton *replaylabel=[[UIButton alloc]initWithFrame:CGRectMake(5, replaylabelY,CGRectGetWidth(self.replayView.frame)-5 , replayH)];
            replaylabel.titleLabel.numberOfLines=0;
            replaylabel.titleLabel.font=[UIFont systemFontOfSize:14];
            [replaylabel setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
            
            NSMutableAttributedString *resultString=[[NSMutableAttributedString alloc]initWithString:showStr];
            [resultString addAttribute:NSForegroundColorAttributeName value:RGBColor(32, 112, 221) range:NSMakeRange(0, replayinfo.user_name.length)];
            if (!strIsEmpty(replayinfo.to_user_name)) {
                [resultString addAttribute:NSForegroundColorAttributeName value:RGBColor(32, 112, 221) range:NSMakeRange(replayinfo.user_name.length+2, replayinfo.to_user_name.length+1)];
            }
            [replaylabel setAttributedTitle:resultString forState:UIControlStateNormal];
            replaylabel.tag = 200 + i;
            [replaylabel addTarget:self action:@selector(deleteReplay:) forControlEvents:UIControlEventTouchUpInside];
            [self.replayView addSubview:replaylabel];
            replaylabelY+=(replayH+5);
        }
    }
    self.lineView.frame=commentframe.lineF;
}
#pragma mark - 删除动态
- (void)deleteCommentReplay{
    if (self.deleteCommentBlack) {
        self.deleteCommentBlack(self.commentframe.commentinfo);
    }
}
#pragma mark - 删除评论
- (void)deleteReplay:(UIButton *)sender{
    ReplayInfo *replayinfo=self.commentframe.commentinfo.comment_list[sender.tag - 200];
    if ([replayinfo.user_id isEqualToString:[JHUserInfo getUserInfo].userId]) {
        if (self.deleteReplayBlack) {
            self.deleteReplayBlack(replayinfo,sender.tag - 200);
        }
    }else{
        if (self.clickTypeBlaock) {
            self.clickTypeBlaock(ClickReplay,self.commentframe.commentinfo,sender.tag - 200);
        }
    }
    
}
#pragma mark - 查看全文
- (void)scanMore:(UIButton *)sender{
//    CommentInfo *commentInfo=self.commentframe.commentinfo;
    if (self.scanMoreBlack) {
        self.scanMoreBlack(self.commentframe.commentinfo);
    }
}
- (void)clickZanOrReplay:(UIButton *)sender{
    if (self.clickTypeBlaock) {
        self.clickTypeBlaock((ClickType)sender.tag,self.commentframe.commentinfo,1000);
    }
}
/**
 *  移除视图
 *
 *  @param supView 父视图
 */
- (void )removeallSubViews:(UIView *)supView{
    for (id obj in supView.subviews) {
        [obj removeFromSuperview];
    }
}
/**
 *  点击图片
 *
 *  @param sender 图片点击事件
 */
- (void)clickImage:(UITapGestureRecognizer *)sender{
    CommentInfo *commentInfo=self.commentframe.commentinfo;
    NSArray *imageList = commentInfo.image_list;
    if (self.scanBigPictureBlack) {
        self.scanBigPictureBlack(imageList,sender.view.tag - 100);
        MyLog(@"%@",imageList[sender.view.tag - 100]);
    }
}
@end
